//
//  HJMSGVC.m
//  project
//
//  Created by hu on 2019/3/21.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJMSGVC.h"
#import "TYTabPagerView.h"
#import "TYTabPagerController.h"
#import "YFChatsVC.h"
#import "YFFriendsVC.h"

@interface HJMSGVC ()<TYTabPagerBarDataSource ,TYTabPagerBarDelegate, TYPagerControllerDataSource, TYPagerControllerDelegate>
/**
 伪导航栏背景View
 */
@property (nonatomic, strong, nonnull) UIView *titleView;
/**
 分页TabBar
 */
@property (nonatomic, strong, nonnull) TYTabPagerBar *tabBar;
/**
 分页控制器
 */
@property (nonatomic, strong, nonnull) TYPagerController *pagerController;
/**
 标题数组
 */
@property (nonatomic, copy, nonnull) NSArray<NSString *> *titles;
/**
 控制器
 */
@property (nonatomic, strong, nonnull) NSArray<UIViewController *> *controllers;

@end

@implementation HJMSGVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self loadData];
}



- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self setupFrames];
}

#pragma mark - 设置UI
- (void)setupViews {
    self.view.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:self.titleView];
    [self setupTitlesTabBar];
    [self setupPagesController];
}

#pragma mark - 加载数据
- (void)loadData {
    [self.tabBar reloadData];
    [self.pagerController reloadData];
}
#pragma mark - 设置 Frame
- (void)setupFrames {
    self.tabBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 45);
    self.pagerController.view.frame = CGRectMake(0, CGRectGetMaxY(self.titleView.frame), self.view.width, self.view.height);
}

#pragma mark - 设置上方标题 TabBar
- (void)setupTitlesTabBar {
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc]init];
    tabBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
    tabBar.layout.normalTextFont = [UIFont boldSystemFontOfSize: 18];
    tabBar.layout.selectedTextFont = [UIFont boldSystemFontOfSize: 18];
    tabBar.layout.normalTextColor = [UIColor colorWithHex:0x999999];
    tabBar.layout.selectedTextColor = [UIColor colorWithHex:0x000000];
    tabBar.layout.progressColor = [UIColor colorWithHex:0xFF2400];
    tabBar.layout.cellWidth = 150 / self.titles.count;
    tabBar.layout.progressHeight = 4;
    tabBar.layout.adjustContentCellsCenter = YES;
    tabBar.layout.cellEdging = 0;
    tabBar.layout.cellSpacing = 0;
    tabBar.dataSource = self;
    tabBar.delegate = self;
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    self.navigationItem.titleView = tabBar;
    _tabBar = tabBar;
}

#pragma mark - 设置PagerController
- (void)setupPagesController {
    TYPagerController *pagerController = [[TYPagerController alloc]init];
    pagerController.layout.prefetchItemCount = 1;
    //pagerController.layout.autoMemoryCache = NO;
    // 只有当scroll滚动动画停止时才加载pagerview，用于优化滚动时性能
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    pagerController.scrollView.bounces = NO;
//    pagerController.scrollView.scrollEnabled = NO;
    pagerController.scrollView.scrollEnabled = YES;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
}

#pragma mark - TYTabPagerBarDataSource ,TYTabPagerBarDelegate
- (NSInteger)numberOfItemsInPagerTabBar {
    return self.titles.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = self.titles[index];
    return cell;
}

- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = self.titles[index];
    return [pagerTabBar cellWidthForTitle:title];
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [self.pagerController scrollToControllerAtIndex:index animate:YES];
}

#pragma mark - TYTabPagerControllerDataSource, TYTabPagerControllerDelegate
- (NSInteger)numberOfControllersInPagerController {
    return self.controllers.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    UIViewController *vc = self.controllers[index];
    vc.view.backgroundColor =
    [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return vc;
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [self.tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
}

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [self.tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}

#pragma mark - Lazy Load
- (NSArray<NSString *> *)titles {
    return @[@"聊天", @"匹配"];
}

- (NSArray<UIViewController *> *)controllers {
    return @[[YFChatsVC new], [YFFriendsVC new]];
}

//- (UIView *)titleView {
//    UIView *view = [UIView new];
//    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT + 44 + 10);
//    UIImageView *image = [UIImageView new];
//    image.frame = CGRectMake(0, 0, view.width, view.height);
//    image.image = [UIImage imageNamed:@"navigationBar"];
//    [view addSubview:image];
//    return  view;
//}


@end
